package Client;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;

public class DrawColumnHistogram {
	public void shuzu(String xuhao) throws SQLException {
		Connection conn = null;
		Statement sql;
		ResultSet rs;

		String xuehao2 = xuhao;

		try {

			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			Class.forName("org.mariadb.jdbc.Driver");
		} catch (ClassNotFoundException e1) {

			e1.printStackTrace();
		}
		conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/login_word", "test", "123");
		sql = conn.createStatement();
		String command1 = "select * from A" + xuehao2;
		rs = sql.executeQuery(command1);

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		// 装载数据
		while (rs.next()) {
			dataset.setValue(rs.getInt(2), xuehao2, rs.getString(1));

		}

		JFreeChart chart = ChartFactory.createBarChart("单词统计图", "单词", "单词数量", dataset, PlotOrientation.VERTICAL, true,
				true, false);

		CategoryPlot plot = chart.getCategoryPlot();
		CategoryAxis domainAxis = plot.getDomainAxis();
		NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

		TextTitle textTitle = chart.getTitle();
		textTitle.setFont(new Font("黑体", Font.PLAIN, 12));
		domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 2));
		domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		numberAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
		numberAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

		try {

			ChartFrame cf = new ChartFrame("柱状图", chart);
			cf.pack();

			cf.setSize(800, 600);

			cf.setVisible(true);

			// ChartUtilities.saveChartAsJPEG(new File("/root/图片/1.png"), chart, 500, 300);
		} catch (Exception e) {
			System.err.println("Problem occurred creating chart.");
		}
	}
}
